#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/11/27 14:16
# @Author  : lizhen
# @Site    : 
# @File    : cMongoDB.py
# @Software: PyCharm
from pymongo import MongoClient


class MongoDBDemo:
    def __init__(self, host=None, port=None):
        self.host = host
        self.port = port
        self.client = None
        self.db = None
        self.collection = None

    def create_connection(self):
        self.client = MongoClient(host=self.host, port=self.port)

    def insert_one(self, db='lz_py', collection='lz', **kwargs):
        self.db = self.client[db]
        self.collection = self.db[collection]
        self.collection.insert_one(kwargs)

    def delete_many(self, db='lz_py', collection='lz', **kwargs):
        self.db = self.client[db]
        self.collection = self.db[collection]
        _result = self.collection.delete_many(kwargs)
        print(_result.deleted_count)

    def update_many(self, db='lz_py', collection='lz', **kwargs):
        self.db = self.client[db]
        self.collection = self.db[collection]
        _field = {key: kwargs[key] for key in kwargs if key != '$set'}
        _data = {key: kwargs[key] for key in kwargs if key == '$set'}
        _result = self.collection.update_many(_field, _data)
        print(_result.matched_count)

    def find_one(self, db='lz_py', collection='lz', **kwargs):
        self.db = self.client[db]
        self.collection = self.db[collection]
        _result = self.collection.find_one(kwargs)
        return _result

    def find(self, db='lz_py', collection='lz', **kwargs):
        self.db = self.client[db]
        self.collection = self.db[collection]
        result = self.collection.find(kwargs)
        for item in result:
            print(item)

    def _test_insert_one(self):
        """
        md = MongoDBDemo(host="192.168.1.101")
        md._test_insert_one()
        """
        md = MongoDBDemo(host=self.host)
        md.create_connection()
        try:
            _item = {'name': 'lizhen'}
            md.insert_one(**_item)
        except Exception as e:
            print(e)

    def _test_delete_many(self):
        """
        md = MongoDBDemo(host="192.168.1.101")
        md._test_delete_many()
        """
        md = MongoDBDemo(host=self.host)
        md.create_connection()
        try:
            _item = {'name': 'lisi'}
            md.delete_many(**_item)
        except Exception as e:
            print(e)

    def _test_update_many(self):
        """
        md = MongoDBDemo(host="192.168.1.101")
        md._test_update_many()
        """
        md = MongoDBDemo(host=self.host)
        md.create_connection()
        try:
            _item = {'name': 'lizhen'}
            _item_update = {'$set': {'name': 'lisi'}}
            md.update_many(**_item, **_item_update)
        except Exception as e:
            print(e)

    def _test_find_one(self):
        """
        md = MongoDBDemo(host="192.168.1.101")
        md._test_find_one()
        """
        md = MongoDBDemo(host=self.host)
        md.create_connection()
        try:
            _item = {'name': 'lizhen'}
            md.find_one(**_item)
        except Exception as e:
            print(e)

    def _test_find(self):
        """
        md = MongoDBDemo(host="192.168.1.101")
        md._test_find()
        """
        md = MongoDBDemo(host=self.host)
        md.create_connection()
        try:
            _item = {'name': 'lizhen'}
            md.find(**_item)
        except Exception as e:
            print(e)


if __name__ == '__main__':
    md = MongoDBDemo(host="192.168.1.101")
    md._test_find()
